package ewms.controller;

import cn.hutool.core.util.StrUtil;
import ewms.common.annotation.Log;
import ewms.common.constant.UserConstants;
import ewms.common.core.controller.BaseController;
import ewms.common.core.domain.AjaxResult;
import ewms.common.enums.BusinessType;
import ewms.common.utils.SecurityUtils;
import ewms.domain.EwmsWasteType;
import ewms.service.IEwmsWasteTypeService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * 废弃物分类信息
 */
@RestController
@RequestMapping("/system/wasteType")
public class EwmsWasteTypeController extends BaseController {
	@Autowired
	private IEwmsWasteTypeService wasteTypeService;

	/**
	 * 获取废弃物分类列表
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteType:list')")
	@GetMapping("/list")
	public AjaxResult list(EwmsWasteType wasteType) {
		List<EwmsWasteType> depts = wasteTypeService.selectWasteTypeList(wasteType);
		return AjaxResult.success(depts);
	}

	/**
	 * 查询废弃物分类列表（排除节点）
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteType:list')")
	@GetMapping("/list/exclude/{wasteTypeId}")
	public AjaxResult excludeChild(@PathVariable(value = "wasteTypeId", required = false) Long wasteTypeId) {
		List<EwmsWasteType> depts = wasteTypeService.selectWasteTypeList(new EwmsWasteType());
		Iterator<EwmsWasteType> it = depts.iterator();
		while (it.hasNext()) {
			EwmsWasteType d = (EwmsWasteType) it.next();
			if (d.getWasteTypeId().intValue() == wasteTypeId
				|| ArrayUtils.contains(StrUtil.split(d.getAncestors(), ","), wasteTypeId + "")) {
				it.remove();
			}
		}
		return AjaxResult.success(depts);
	}

	/**
	 * 根据废弃物分类编号获取详细信息
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteType:query')")
	@GetMapping(value = "/{wasteTypeId}")
	public AjaxResult getInfo(@PathVariable Long wasteTypeId) {
		return AjaxResult.success(wasteTypeService.selectWasteTypeById(wasteTypeId));
	}

	/**
	 * 获取废弃物分类下拉树列表
	 */
	@GetMapping("/treeselect")
	public AjaxResult treeselect(EwmsWasteType wasteType) {
		List<EwmsWasteType> wasteTypeList = wasteTypeService.selectWasteTypeList(wasteType);
		return AjaxResult.success(wasteTypeService.buildWasteTypeTreeSelect(wasteTypeList));
	}

	/**
	 * 获取废弃物分类下拉树列表
	 */
	@GetMapping("/treeselectAll")
	public AjaxResult treeselectAll() {
		List<EwmsWasteType> wasteTypeList = wasteTypeService.selectAllWasteList();
		return AjaxResult.success(wasteTypeService.buildWasteTypeTreeSelect(wasteTypeList));
	}

	/**
	 * 新增废弃物分类
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteType:add')")
	@Log(title = "废弃物分类管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody EwmsWasteType wasteType) {
		if (UserConstants.NOT_UNIQUE.equals(wasteTypeService.checkWasteTypeNameUnique(wasteType))) {
			return AjaxResult.error("新增废弃物分类'" + wasteType.getWasteTypeName() + "'失败，废弃物分类名称已存在");
		}
		wasteType.setCreateBy(SecurityUtils.getUsername());
		return toAjax(wasteTypeService.insertWasteType(wasteType));
	}

	/**
	 * 修改废弃物分类
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteType:edit')")
	@Log(title = "废弃物分类管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody EwmsWasteType wasteType) {
		if (UserConstants.NOT_UNIQUE.equals(wasteTypeService.checkWasteTypeNameUnique(wasteType))) {
			return AjaxResult.error("修改废弃物分类'" + wasteType.getWasteTypeName() + "'失败，废弃物分类名称已存在");
		} else if (wasteType.getParentId().equals(wasteType.getWasteTypeId())) {
			return AjaxResult.error("修改废弃物分类'" + wasteType.getWasteTypeName() + "'失败，上级废弃物分类不能是自己");
		} else if (StrUtil.equals(UserConstants.DEPT_DISABLE, wasteType.getStatus())
			&& wasteTypeService.selectNormalChildrenWasteTypeById(wasteType.getWasteTypeId()) > 0) {
			return AjaxResult.error("该废弃物分类包含未停用的子废弃物分类！");
		}
		wasteType.setUpdateBy(SecurityUtils.getUsername());
		return toAjax(wasteTypeService.updateWasteType(wasteType));
	}

	/**
	 * 删除废弃物分类
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteType:remove')")
	@Log(title = "废弃物分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wasteTypeId}")
	public AjaxResult remove(@PathVariable Long wasteTypeId) {
		if (wasteTypeService.hasChildByWasteTypeId(wasteTypeId)) {
			return AjaxResult.error("存在下级废弃物分类,不允许删除");
		}
		return toAjax(wasteTypeService.deleteWasteTypeById(wasteTypeId));
	}
}
